import fs from 'fs'
import path from 'path'

export function getConfig () {
  const notePath = path.join(process.env.HOME as string, '.znote')
  let exist = fs.existsSync(notePath)
  if (!exist) {
    fs.mkdirSync(notePath)
  }

  let configFile = path.join(notePath, 'config.json')
  exist = fs.existsSync(configFile)
  let config:any = {
    'NoteRoot': path.join(process.env.HOME as string, 'znote')
  }

  if (exist) {
    config = fs.readFileSync(configFile, 'utf-8')
    config = JSON.parse(config)
  } else {
    fs.writeFileSync(configFile, JSON.stringify(config))
  }

  exist = fs.existsSync(config.NoteRoot)
  if (!exist) {
    fs.mkdirSync(config.NoteRoot)
  }

  return config
}

export default getConfig
